function init()
{
    GamesTreeView.init();
    document.getElementById('games').view = GamesTreeView;

    var hookGyaASE = false;
    var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    try {
        hookGyaASE = prefManager.getBoolPref("extensions.ellaunch.hook_gyaase");
    } catch (e) {
        hookGyaASE = false;
    }
    document.getElementById('hook_gyaase').checked = hookGyaASE;
}

function complete()
{
}

function addGame()
{
    var result = {};
    openDialog("prefs_addgame.xul", "_blank", "chrome,centerscreen,modal", result);
    if (!("protocol" in result)) return true;
    GamesTreeView.addGameInfo(result);
    return true;
}

function editGame()
{
    var result = {};
    var game_index = GamesTreeView.selection.currentIndex;
    var game_info = GamesTreeView.getGameInfo(game_index);
    openDialog("prefs_editgame.xul", "_blank", "chrome,centerscreen,modal", game_info, result);
    if (!("name" in result)) return true;

    GamesTreeView.setGameInfo(game_index, result);
    return true;
}

function delGame()
{
    var game_index = GamesTreeView.selection.currentIndex;
    var game_info = GamesTreeView.getGameInfo(game_index);
    if (!window.confirm(game_info.name + "を削除しますか？")) return true;

    GamesTreeView.delGameInfo(game_index);

    return true;
}

function saveChanges()
{
    GamesTreeView.saveChanges();
    var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    prefManager.setBoolPref("extensions.ellaunch.hook_gyaase", document.getElementById('hook_gyaase').checked);
}

var GamesTreeView = {
    _data: null,

    init : function()
    {
        this._data = new Array();

        var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        var games = new Array();
        try {
            var data = prefManager.getComplexValue('extensions.ellaunch.games', Components.interfaces.nsISupportsString).data;
            games = data.split(',');
        } catch (e) {
            games = [];
        }
        for (var i = 0; i < games.length; i++) {
            var path = null;
            var args = null;
            var name = null;
            try {
                name = prefManager.getComplexValue('extensions.ellaunch.game_' + games[i] + '_name', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                continue;
            }
            try {
                path = prefManager.getComplexValue('extensions.ellaunch.game_' + games[i] + '_path', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                path = '';
            }
            try {
                args = prefManager.getComplexValue('extensions.ellaunch.game_' + games[i] + '_args', Components.interfaces.nsISupportsString).data;
            } catch (e) {
                args = '';
            }
            this._data[i] = {protocol: games[i], name: name, path: path, args: args};
        }
    },
    get rowCount()
    {
        return this._data.length;
    },
    getCellText : function(row, col)
    {
        switch (col.index) {
            case 0: return this._data[row].name;
            case 1: return this._data[row].path;
            case 2: return this._data[row].args;
        }
        return '';
    },
    setTree: function(treebox)
    {
        this.treebox = treebox;
    },
    isContainer: function(row){ return false; },
    isSeparator: function(row){ return false; },
    isSorted: function(){ return false; },
    getLevel: function(row){ return 0; },
    getImageSrc: function(row,col){ return null; },
    getRowProperties: function(row,props){},
    getCellProperties: function(row,col,props){},
    getColumnProperties: function(colid,col,props){},
    cycleHeader: function(col) {},
    getGameInfo: function(index)
    {
        return this._data[index];
    },
    setGameInfo: function(index, game_info)
    {
        this._data[index] = game_info;
        this.treebox.invalidate();
    },
    addGameInfo: function(game_info)
    {
        this._data.push(game_info);
        this.treebox.rowCountChanged(this.rowCount + 1, 1);
    },
    delGameInfo: function(index)
    {
        this._data.splice(index, 1);
        this.treebox.rowCountChanged(index, -1);
    },
    existsGameProtocol: function(protocol)
    {
        for (var i = 0; i < this._data.length; i++) {
            if (this._data[i].protocol == protocol) return true;
        }
        return false;
    },
    saveChanges: function()
    {
        var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        var games = new Array();
        try {
            var data = prefManager.getComplexValue('extensions.ellaunch.games', Components.interfaces.nsISupportsString).data;
            games = data.split(',');
        } catch (e) {
            games = [];
        }

        for (var i = 0; i < games.length; i++) {
            try {
                prefManager.clearUserPref('extensions.ellaunch.game_' + games[i] + '_name');
            } catch (e) {
            }
            try {
                prefManager.clearUserPref('extensions.ellaunch.game_' + games[i] + '_path');
            } catch (e) {
            }
            try {
                prefManager.clearUserPref('extensions.ellaunch.game_' + games[i] + '_args');
            } catch (e) {
            }
        }

        games = [];

        for (var i = 0; i < this._data.length; i++) {
            var name = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            name.data = this._data[i].name;
            prefManager.setComplexValue('extensions.ellaunch.game_' + this._data[i].protocol + '_name', Components.interfaces.nsISupportsString, name);
            var path = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            path.data = this._data[i].path;
            prefManager.setComplexValue('extensions.ellaunch.game_' + this._data[i].protocol + '_path', Components.interfaces.nsISupportsString, path);
            var args = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            args.data = this._data[i].args;
            prefManager.setComplexValue('extensions.ellaunch.game_' + this._data[i].protocol + '_args', Components.interfaces.nsISupportsString, args);
            games.push(this._data[i].protocol);
        }
        var games2 = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        games2.data = games.join(',');
        prefManager.setComplexValue('extensions.ellaunch.games', Components.interfaces.nsISupportsString, games2);
    }
};

